{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T03:32:25.426808Z",
     "start_time": "2025-11-11T03:32:25.287579Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "efb25b503c1c8062",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-11T03:33:48.363597Z",
     "start_time": "2025-11-11T03:33:48.357833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def AND(x1,x2):\n",
    "    x=np.array([x1,x2])\n",
    "    w=np.array([0.5,0.5])\n",
    "    b=-0.7\n",
    "    tmp=np.sum(w*x)+b\n",
    "    if tmp<=0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "print(AND(0,1))  # 输出 0\n",
    "print(AND(1,0))  # 输出 0\n",
    "print(AND(1,1))  # 输出 1\n",
    "print(AND(0,0))  # 输出 0"
   ],
   "id": "db88124bfb8944a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T03:36:41.935350Z",
     "start_time": "2025-11-11T03:36:41.922853Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def NAND(x1,x2):\n",
    "    x=np.array([x1,x2])\n",
    "    w=np.array([-0.5,-0.5])\n",
    "    b=0.7\n",
    "    tmp=np.sum(w*x)+b\n",
    "    if tmp<=0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "print(NAND(0,1))\n",
    "print(NAND(1,0))\n",
    "print(NAND(1,1))\n",
    "print(NAND(0,0))"
   ],
   "id": "eb52287a34867edf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T03:39:12.339052Z",
     "start_time": "2025-11-11T03:39:12.335943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def OR(x1,x2):\n",
    "    x=np.array([x1,x2])\n",
    "    w=np.array([0.5,0.5])\n",
    "    b=-0.3\n",
    "    tmp=np.sum(w*x)+b\n",
    "    if tmp<=0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "print(OR(0,1))\n",
    "print(OR(1,0))\n",
    "print(OR(1,1))\n",
    "print(OR(0,0))"
   ],
   "id": "a82668934458d461",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T03:46:52.865038Z",
     "start_time": "2025-11-11T03:46:52.860055Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def XOR(x1,x2):\n",
    "    s1=NAND(x1,x2)\n",
    "    s2=OR(x1,x2)\n",
    "    return AND(s1,s2)\n",
    "print(XOR(0,0))\n",
    "print(XOR(0,1))\n",
    "print(XOR(1,0))\n",
    "print(XOR(1,1))\n"
   ],
   "id": "920a9043ec92565c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "execution_count": 21
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
